﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using System.Data.SqlClient;
using System.Data;

namespace ZX.WebApp.Crm
{
    /// <summary>
    /// System.Web.UI.Page
    /// </summary>
    public partial class SendNews : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnsend_Click(object sender, EventArgs e)
        {
            string Content = txtMODEL.Value;

            if (Content.Length > 500)
            {
                Labmes.Text = "字数限制：消息字数超过500，请裁剪后发送！";
                return;
            }
            if (Save(Content) == 1)
                Labmes.Text = "发送成功！";
            else
                Labmes.Text = "发送失败！";
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="Content">内容</param>
        /// <returns></returns>
        public int Save(string Content)
        {
            try
            {
                ZX.EHR.Library.DatUser u = (Session["User"] as ZX.EHR.Library.DatUser);
                ///发送人编号
                Guid AddUser = u.UserID;
                ///发送人名称
                string createPeopleName = u.UName;
                ///接收人
                string ReceiverIds = PM.Value;
                ///接收人名称
                string recevePeopleName = txtPM.Value;
                ///消息标题
                string title = txtTitle.Value;
                ///发送方式
                string sendMode = ddlSendmode.SelectedValue;

                #region 数据库代码
                ///定义字符串
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("insert into DatSysNews(AddUser,createPeopleName,recevePeopleName,title,content,sendMode)");
                sb.Append("values(@AddUser,@createPeopleName,@recevePeopleName,@title,@content,@sendMode);");
                sb.Append("declare @Nid uniqueidentifier;select top 1 @Nid = ID from DatSysNews order by createDate desc;");
                //添加子表信息
                string[] str = ReceiverIds.TrimEnd(',').Split(',');
                for (int i = 0; i < str.Length; i++)
                {
                    sb.Append("insert into DatSysNewsInfo(Nid,recevePeopleID) values(@Nid,@recevePeopleID" + i + ");");
                }

                //打开数据库
                string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand com = new SqlCommand(sb.ToString(), conn);
                    com.Parameters.Add("AddUser", SqlDbType.UniqueIdentifier);
                    com.Parameters["AddUser"].Value = AddUser;
                    com.Parameters.Add("createPeopleName", SqlDbType.VarChar, 50);
                    com.Parameters["createPeopleName"].Value = createPeopleName;

                    com.Parameters.Add("recevePeopleName", SqlDbType.VarChar, 8000);
                    com.Parameters["recevePeopleName"].Value = recevePeopleName;
                    com.Parameters.Add("title", SqlDbType.VarChar, 50);
                    com.Parameters["title"].Value = title;

                    com.Parameters.Add("content", SqlDbType.VarChar, 500);
                    com.Parameters["content"].Value = Content;
                    com.Parameters.Add("sendMode", SqlDbType.VarChar, 50);
                    com.Parameters["sendMode"].Value = sendMode;

                    //给字表的参数赋值
                    for (int i = 0; i < str.Length; i++)
                    {
                        com.Parameters.Add("recevePeopleID" + i, SqlDbType.UniqueIdentifier);
                        com.Parameters["recevePeopleID" + i].Value = Guid.Parse(str[i]);
                    }

                    int wolf = com.ExecuteNonQuery();
                    if (wolf > 0)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
                #endregion
            }
            catch (Exception se)
            {
                //抓取一场信息 并提示
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('错误：" + se.ToString() + "');", true);
                return 0;
            }
        }
    }
}